package com.tgit.sso.paltform.common;

import java.util.List;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.tgit.sso.core.common.ResponseT;
import com.tgit.sso.core.conf.SsoException;
import com.fasterxml.jackson.core.JsonParseException;
/**
 *
* @ClassName: GlobalExceptionHandler   
* @Description: ( 全局异常切面，json格式做返回)   
* @author 范再军  
* @date 2018年4月17日 上午9:24:02   
*
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	
	private static final String JSON = "json";
	private static final String JSON_ERROR_MSG = "json解析异常";
	private static final String SERVER_ERROR = "服务器错误!";

	/**
	  * 服务器错误异常
	  * @param exception
	  * @return
	  */
	 @ResponseBody
	 @ExceptionHandler(Exception.class)  
     public Object runtimeExceptionHandler(Exception exception) {  
        // 打印 报错堆栈轨迹  
        exception.printStackTrace();  
        ResponseT<String> res = new ResponseT<String>(null);
        res.setCode(ResponseT.FAIL_CODE);;
        res.setMsg(SERVER_ERROR);
        return res;  
     }  
	 /**
	  * 入参解析错误异常
	  * @param exception
	  * @return
	  */
	 @ResponseBody
	 @ExceptionHandler(MethodArgumentNotValidException.class)  
	 public Object runtimeExceptionHandler(MethodArgumentNotValidException exception) {  
		 // 打印 报错堆栈轨迹  
		 exception.printStackTrace();  
		 List<FieldError> errorList=exception.getBindingResult().getFieldErrors();
		 //遍历方式1：Iterator it=errorList.iterator();
		 //遍历方式2：每次只提示一次错误
		 ResponseT<String> res = new ResponseT<String>(null);
		 for (FieldError fieldError : errorList) {
		 	res.setCode(500);
			res.setMsg(fieldError.getDefaultMessage());
			res.setField(fieldError.getField());
			break;
		 }
		 return res;  
	 }  
	 
	 /**
	  * json解析异常
	  */
	 @ResponseBody
	 @ExceptionHandler(SsoException.class)  
	 public Object runtimeExceptionHandler(SsoException exception) {  
		 // 打印 报错堆栈轨迹  
		 exception.printStackTrace();
		 ResponseT<String> res = new ResponseT<String>(null);
		 res.setField(exception.getMessage());
		 res.setCode(500);
		 return res;  
	 }  
	 /**
	  * json解析异常
	  */
	 @ResponseBody
	 @ExceptionHandler(JsonParseException.class)  
	 public Object runtimeExceptionHandler(JsonParseException exception) {  
		 // 打印 报错堆栈轨迹  
		 exception.printStackTrace();  
		 ResponseT<String> res = new ResponseT<String>(null);
		 res.setMsg(JSON_ERROR_MSG);
		 res.setField(JSON);
		 res.setCode(500);
		 return res;  
	 }  
	 /**
	  * json解析异常
	  */
	 @ResponseBody
	 @ExceptionHandler(HttpMessageNotReadableException.class)  
	 public Object runtimeExceptionHandler(HttpMessageNotReadableException exception) {  
		 // 打印 报错堆栈轨迹  
		 exception.printStackTrace();  
		 ResponseT<String> res = new ResponseT<String>(null);
		 res.setMsg(JSON_ERROR_MSG);
		 res.setField(JSON);
		 res.setCode(500);
		 return res;  
	 }  
	 
}
